<template>
  <el-main>
    <div class="margin-bottom-20">
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/device' }"
          >设备管理
        </el-breadcrumb-item>
        <el-breadcrumb-item>库存设备</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="clearfix">
      <el-form class="pull-left" inline>
        <el-form-item>
          <el-input
            placeholder="请输入imei"
            v-model="search_param.imei"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-select placeholder="设备类型" v-model="search_param.deviceType">
            <el-option
              :key="item.code"
              :label="item.label"
              :value="item.code"
              v-for="item in device_type"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-select placeholder="通信方式" v-model="search_param.deviceSocket">
            <el-option
              :key="item.code"
              :label="item.label"
              :value="item.code"
              v-for="item in device_socket"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-select placeholder="平台信息" v-model="search_param.platformType">
            <el-option
              :key="item.code"
              :label="item.label"
              :value="item.code"
              v-for="item in platform_type"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button-group>
            <el-button
              @click="handleSearch"
              icon="el-icon-search"
              size="medium"
              type="primary"
              >搜索
            </el-button>
            <el-button @click="handleReset" size="medium" type="primary"
              >重置
            </el-button>
          </el-button-group>
        </el-form-item>
      </el-form>
      <el-form class="pull-right" inline>
        <el-form-item>
          <!--                    <el-button-group>-->
          <!--                        <el-button @click="handleAdd" size="medium" type="primary"-->
          <!--                        >注册设备-->
          <!--                        </el-button>-->
          <!--                        <el-button size="medium" type="primary">批量分配</el-button>-->
          <!--                    </el-button-group>-->
          <el-dropdown
            @click="handleAdd"
            @command="handleCommand"
            size="medium"
            split-button
            type="primary"
          >
            注册设备
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="handleAddBatch"
                >批量注册
              </el-dropdown-item>
              <el-dropdown-item command="showBatchBind"
                >批量分配</el-dropdown-item
              >
            </el-dropdown-menu>
          </el-dropdown>
        </el-form-item>
      </el-form>
    </div>
    <el-card class="box-card" shadow="never">
      <el-table
        :data="table_data"
        @selection-change="handleSelectionChange"
        stripe
        style="width: 100%"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column
          label="设备类型"
          prop="deviceType_desc"
          show-overflow-tooltip
          width="120"
        />
        <el-table-column label="imei" prop="imei" show-overflow-tooltip />
        <el-table-column
          label="通信方式"
          prop="deviceSocket_desc"
          show-overflow-tooltip
          width="120"
        />
        <el-table-column
          label="平台信息"
          prop="platformType_desc"
          show-overflow-tooltip
        />
        <el-table-column
          label="设备波段(MHz)"
          prop="waveLength"
          show-overflow-tooltip
        />
        <el-table-column label="注册时间" prop="ctime" width="160" />
        <el-table-column label="操作" width="120">
          <template slot-scope="scope">
            <!--                        <el-button @click="handleEdit(scope.row)" size="mini" type="text"-->
            <!--                        >修改-->
            <!--                        </el-button>-->
            <el-button
              @click="handleAllocation(scope.row)"
              size="mini"
              type="text"
              >分配
            </el-button>
            <el-button @click="handleDelete(scope.row)" size="mini" type="text"
              >删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="margin-top-10 clearfix">
        <el-pagination
          :current-page.sync="current_page"
          :page-size.sync="page_size"
          :page-sizes="[5, 10, 20]"
          :total="total_size"
          @current-change="load_table_data"
          @size-change="load_table_data"
          background
          class="pull-right"
          layout="total, sizes,  prev, pager, next"
        />
      </div>
    </el-card>

    <el-dialog :visible.sync="dialogVisible" title="批量分配" width="50%">
      <div style="padding: 20px 20px 100px">
        <el-form label-width="120px">
          <el-form-item label="所属企业">
            <el-select
              placeholder="请选择所属企业"
              v-model="batchForm.companyId"
            >
              <el-option
                :key="item.id"
                :label="item.companyName"
                :value="item.id"
                v-for="item in companyList"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="展示WEB页面" prop="toWeb">
            <el-switch
              active-text="是"
              inactive-text="否"
              v-model="batchForm.toWeb"
            ></el-switch>
          </el-form-item>
        </el-form>
      </div>
      <div slot="footer">
        <el-button @click="handleBatchAllocation" type="primary"
          >确 定
        </el-button>
        <el-button @click="dialogVisible = false">取 消</el-button>
      </div>
    </el-dialog>
  </el-main>
</template>

<script>
import { mapActions, mapState } from "vuex";

export default {
  data() {
    return {
      dialogVisible: false,
      multipleSelection: [],
      batchForm: { companyId: null, toWeb: false },
      companyList: [],
      device_type: [],
      device_socket: [],
      platform_type: []
    };
  },
  computed: {
    ...mapState("device_unbind", ["total_size", "table_data"]),
    page_size: {
      get() {
        return this.$store.state.device_unbind.page_size;
      },
      set(value) {
        this.set_page_size(value);
      }
    },
    current_page: {
      get() {
        return this.$store.state.device_unbind.current_page;
      },
      set(value) {
        this.set_current_page(value);
      }
    },
    search_param: {
      get() {
        return this.$store.state.device_unbind.search_param;
      }
    }
  },
  mounted() {
    this.load_table_data();
    this.load_device_type();
    this.load_device_socket();
    this.load_platform_type();
  },
  methods: {
    ...mapActions("device_unbind", [
      "load_table_data",
      "set_page_size",
      "set_current_page",
      "set_search_param"
    ]),
    handleSearch() {
      this.set_search_param(Object.assign({}, this.search_param));
      this.load_table_data();
    },
    handleReset() {
      this.set_search_param({});
      this.load_table_data();
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    handleAdd() {
      this.$router.push("/device/unbind/add");
    },
    handleCommand(command) {
      if (command === "handleAddBatch") {
        this.$router.push("/device/unbind/add/batch");
        return;
      }

      if (command === "showBatchBind") {
        this.showBatchAllocation();
        return;
      }
      this.$message("click on item " + command);
    },
    handleEdit(row) {
      this.$router.push("/device/unbind/edit/" + row.id);
    },
    handleAllocation(row) {
      this.$router.push("/device/unbind/allocation/" + row.id);
    },
    showBatchAllocation() {
      if (
        this.multipleSelection == null ||
        this.multipleSelection.length <= 0
      ) {
        this.$message({
          message: "请选择设备",
          type: "warning"
        });
        return;
      }
      this.companyOptions();
      this.batchForm.companyId = null;
      this.batchForm.toWeb = false;
      this.dialogVisible = true;
    },
    handleBatchAllocation() {
      if (
        this.multipleSelection == null ||
        this.multipleSelection.length <= 0
      ) {
        this.$message({
          message: "请选择设备",
          type: "warning"
        });
        return;
      }

      const ids = this.multipleSelection.map(item => {
        return item.id;
      });

      if (this.batchForm.companyId == null) {
        this.$message({
          message: "请选择所属企业",
          type: "warning"
        });
        return;
      }

      let loadingInstance = this.$loading({ fullscreen: true });
      this.$ajax.device_unbind
        .allocation_batch({
          ids: ids,
          companyId: this.batchForm.companyId,
          toWeb: this.batchForm.toWeb
        })
        .then(result => {
          if (result.code != 1) {
            this.$message({
              message: result.msg,
              type: "warning"
            });
          } else {
            this.$message({
              type: "success",
              message: "分配设备成功!"
            });

            this.batchForm.companyId = null;
            this.batchForm.toWeb = false;
            this.dialogVisible = false;
            this.load_table_data();
          }
        })
        .catch(e => {
          console.log(e);
          this.$message({
            message: "系统繁忙，请稍后重试",
            type: "warning"
          });
        })
        .finally(function() {
          loadingInstance.close();
        });
    },
    handleDelete(row) {
      this.$confirm(
        "此操作将永久删除设备：" + row.imei + " , 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      ).then(() => {
        let loadingInstance = this.$loading({ fullscreen: true });
        this.$ajax.device_unbind
          .remove(row.id)
          .then(result => {
            if (result.code != 1) {
              this.$message({
                message: result.msg,
                type: "warning"
              });
            } else {
              this.$message({
                type: "success",
                message: "删除成功!"
              });
              this.load_table_data();
            }
          })
          .catch(e => {
            console.log(e);
            this.$message({
              message: "系统繁忙，请稍后重试",
              type: "warning"
            });
          })
          .finally(function() {
            loadingInstance.close();
          });
      });
    },
    load_device_type() {
      this.$ajax.support
        .device_type()
        .then(result => {
          if (result.code === 1) {
            this.device_type = result.data;
          }
        })
        .catch(e => {
          console.log(e);
        });
    },
    load_device_socket() {
      this.$ajax.support
        .device_socket()
        .then(result => {
          if (result.code === 1) {
            this.device_socket = result.data;
          }
        })
        .catch(e => {
          console.log(e);
        });
    },
    load_platform_type() {
      this.$ajax.support
        .platform_type()
        .then(result => {
          if (result.code === 1) {
            this.platform_type = result.data;
          }
        })
        .catch(e => {
          console.log(e);
        });
    },
    companyOptions() {
      this.$ajax.company
        .options()
        .then(result => {
          if (result.code == 1) {
            this.companyList = result.data;
          } else {
            this.$message({
              message: result.msg,
              type: "warning"
            });
          }
        })
        .catch(e => {
          console.log(e);
          this.$message({
            message: "系统繁忙，请稍后重试",
            type: "warning"
          });
        });
    }
  }
};
</script>
